<?php 
namespace Home\Controller;
use  Think\Controller;

class UserController extends Controller{
 

  //注册
  public function reg(){
   if(!IS_POST){ 	
    	$this->display('login');
   }else{
   	//new Model
      $user=D('Home/user');

      if(!$user->create()){
        $this->display('login');
      }else{
      	$user->reg();
      	echo '<script>alert(\'注册成功\')</script>';
  	   exit('<meta http-equiv="refresh" content="0;url=http:login">');	   
      }        	
    }
  }




//登录
  public function  login(){
  	  $user=D('Home/user');
    	 if(!IS_POST){
    		$this->display();
    	 }else{

    	 	$u=$user->where(array('username'=>I('post.username')))->find();
    	 	//print_r($u);
    	 	//检测用户是否存在，不存在直接跳转到login
     		if(empty($u)){
     		 echo "<script>alert('用户名不存在')</script>";
     		 exit('<meta http-equiv="refresh" content="0;url=http:login")">');
     		}

		
     		//验证密码是否正确
   		if($user->checkPwd(I('post.password'))){

        
   		$user->auth();
        echo '<script>alert(\'登录成功,将返回主页\')</script>';
   		  exit('<meta http-equiv="refresh" content="0;url=/index.php")">');
      }else{
        echo "<script>alert('用户名或密码错误')</script>";
        exit('<meta http-equiv="refresh" content="0;url=http:"login")">');
   	
   		}
     		
     }
  }

  public function userexit(){
      cookie('user_id',null);
      cookie('user',null);
      cookie('pwd',null);
      $this->ajaxReturn('已安全退出','eval');
  }

}





 ?>